﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using Janus.Windows.GridEX;

namespace Company.Portal.Win
{
    public partial class BusinessObjectListControl : BaseControl
    {
        public BusinessObjectListControl()
        {
            InitializeComponent();


        }
        public void AddDataToGrid(List<object> items)
        {
            if (items == null || items.Count == 0)
                return;
            DataTable dataTable = new DataTable();
          
            var propertyinfos = DataListAdapter.GetProperties(items[0]);
            foreach (KeyValuePair<string, Type> keyValuePair in propertyinfos)
            {
                dataTable.Columns.Add(new DataColumn(keyValuePair.Key, keyValuePair.Value));
                
             
            }
            foreach (var ins in items)
            {
              var x=  dataTable.NewRow() ;
                foreach (KeyValuePair<string, Type> keyValuePair in propertyinfos)
                {
                    x[keyValuePair.Key] = ins.GetType().InvokeMember(keyValuePair.Key, BindingFlags.GetProperty, null,
                                                                     ins, null); 
                    
                }
                dataTable.Rows.Add(x);
            }
            
            gridEX1.DataSource = dataTable;
            gridEX1.RightToLeft = RightToLeft.Yes;
            gridEX1.DataMember = "RelatedObject.Descriptor";
           // gridEX1.DefaultAlphaMode = AlphaMode.Transparent;
            GridEXLayout gridExLayout=new GridEXLayout();
            gridEX1.AllowEdit = InheritableBoolean.False;
          //  gridEX1.Layouts.Add(gridExLayout);
            //gridEX1.CurrentLayout = gridExLayout;
         var l=   gridEX1.GetLayout();
            //gridEX1.CurrentLayout=
            gridEX1.RetrieveStructure();
           
        }

        private void toolStripButtonLayout_Click(object sender, EventArgs e)
        {
            XmlDocument xmlDocument = new XmlDocument();
            var l = gridEX1.GetLayout();
            xmlDocument.LoadXml(l.LayoutString);

            //LayoutSetting layoutSetting=new LayoutSetting();
            //layoutSetting.Layout = gridEX1.GetLayout();
            //layoutSetting.GridEx = gridEX1;
            //layoutSetting.ParentControl = this;
            //layoutSetting.Show();
            gridEX1.Layouts.Add(l);
            gridEX1.CurrentLayout = l;
            GridEXLayout gridExLayout = gridEX1.CurrentLayout.CloneLayout(true);
            gridEX1.Layouts.Clear();
            gridEX1.Layouts.Add(gridExLayout);
            gridEX1.CurrentLayout = gridExLayout;
            var xl = gridEX1.GetLayout();
           xmlDocument.LoadXml(xl.LayoutString);
         //   gridEX1.RetrieveStructure();
           gridEX1.Refresh();
            gridEX1.CurrentLayout.Update();
           gridEX1.LoadLayout(gridExLayout);
              gridEX1.Layouts.Add(xl);
        }

        public GridEX GridEx
        {
            get { return gridEX1; }
            set { gridEX1 = value;
            
            }
        }

    }
}
